<?php
namespace app\index\controller;

use think\Controller;

class Es extends Controller
{

    public function __construct()
    {
        //Vendor('Elasticsearch.autoload');
        require '../extend/Elasticsearch/autoload.php';
        $params['hosts'] = array(
            'es-cn-v641e327m0046twih.public.elasticsearch.aliyuncs.com:9200',

        );
   

        $this->client = new \Elasticsearch\Client($params);

    }

    public function create_index()
    {
      
        $indexParams['index'] = 'dianshu';
        $indexParams['type'] = 'goods';
        $indexParams['body']['settings']['number_of_shards'] = 2;
        $indexParams['body']['settings']['number_of_replicas'] = 0;
//dump($indexParams);exit;
//
         $params = [
            'index' => "dianshu",
            'type' => "test",
            'body' => [
                "dian" => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'id' => [
                            'type' => 'integer', // 整型
            
                        ],
                        'name' => [
                            'type' => 'text', // 字符串型
                           
                        ],
                        'profile' => [
                            'type' => 'text',
                           
                        ],
                        'age' => [
                            'type' => 'integer',
                        ],
                    ]
                ]
            ]
        ]; 
 
//dump(json_encode($params));exit;
       dump($this->client->create($params));exit;
    }

    public function add_document()
    {
        $params = array();
        $params['body'] = array(
            'testField' => 'dfdsfdsf'
        );
        $params['index'] = 'my_index';
        $params['type'] = 'my_index';
        $params['id'] = 'w1231313';
        $ret = $this->client->index($params);
    }

    public function delete_index()
    {
        $deleteParams['index'] = 'my_index';
        $this->client->indices()->delete($deleteParams);
    }

    public function delete_document()
    {
        $deleteParams = array();
        $deleteParams['index'] = 'my_index';
        $deleteParams['type'] = 'my_index';
        $deleteParams['id'] = 'AU4Kmmj-WOmOrmyOj2qf';
        $retDelete = $this->client->delete($deleteParams);
    }
    public function update_document()
    {
        $updateParams = array();
        $updateParams['index'] = 'my_index';
        $updateParams['type'] = 'my_index';
        $updateParams['id'] = 'my_id';
        $updateParams['body']['doc']['asas']  = '111111';
       $response = $this->client->update($updateParams);
         
    }
    public function search()
    {
        $searchParams['index'] = 'my_index';
        $searchParams['type'] = 'my_index';
        $searchParams['from'] = 0;
        $searchParams['size'] = 100;
        $searchParams['sort'] = array(
            '_score' => array(
                'order' => 'desc'
            )
        );
        // $searchParams['body']['query']['match']['testField'] = 'abc';
        $retDoc = $this->client->search($searchParams);
        print_r($retDoc);
    }

    public function get_document()
    {
        $getParams = array();
        $getParams['index'] = 'my_index';
        $getParams['type'] = 'my_index';
        $getParams['id'] = 'AU4Kn-knWOmOrmyOj2qg';
        $retDoc = $this->client->get($getParams);
        print_r($retDoc);
    }
}
?>